package com.ldj.struts.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.hibernate.DataDictionary;
import com.ldj.hibernate.DataDictionaryDAO;
import com.ldj.hibernate.EmployerInfo;
import com.ldj.hibernate.EmployerInfoDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.hibernate.UnitUser;
import com.ldj.hibernate.UnitUserDAO;
import com.ldj.hibernate.UnitUserRegisterDAO;
import com.ldj.struts.form.UnitUserForm;

public class UnitUserAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UnitUserDAO dao = new UnitUserDAO(request, response);
		DataDictionaryDAO ddDAO = new DataDictionaryDAO(request, response);
		List list = null;
		UnitUserForm uForm = (UnitUserForm)form;
		String empInfoName = request.getParameter("empInfoName");
		String areaCode = request.getParameter("areaCode");
		/*String empInfoPlaceOrganCode ="";
		if(empInfoPlaceOrgan!=null && !"".equals(empInfoPlaceOrgan)){
			DataDictionary dd = ddDAO.findByName(empInfoPlaceOrgan);
			empInfoPlaceOrganCode=dd.getDataDictionaryCode();*/
			/*if(empInfoPlaceOrganCode==null || "".equals(empInfoPlaceOrganCode)){
				request.setAttribute("error","您所输入的区域名称不存在，请重新输入！" );
				return mapping.findForward("error");
			}*/
		list = dao.findAllPage(areaCode,uForm,empInfoName);
		request.setAttribute("intPage", String.valueOf(dao.intPage));
		request.setAttribute("list", list);
		return mapping.findForward("list");
	}
	
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		String eid= request.getParameter("eid");
		EmployerInfo eInfo = dao.findById(eid);
		request.setAttribute("eInfo", eInfo);
		return mapping.findForward("toAdd");
	}
	
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UnitUserDAO dao = new UnitUserDAO(request, response);
		UnitUser info = dao.findId(request.getParameter("uid"));
		request.setAttribute("uInfo", info);
		request.setAttribute("nowPage",request.getParameter("page"));
		return mapping.findForward("toUpdate");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UnitUserForm uForm = (UnitUserForm)form;
		UnitUserDAO dao = new UnitUserDAO(request, response);
		UnitUser info = dao.findId(request.getParameter("uid"));
		EmployerInfoDAO eDao = new EmployerInfoDAO(request, response);
		EmployerInfo eInfo = eDao.findById(uForm.getUnitUserUnitId());
		
		info.setUnitUser(eInfo);
	//	info.setUnitUserLoginName(uForm.getUnitUserLoginName());
		info.setUnitUserPassword(uForm.getUnitUserPassword());
		info.setUnitUserName(uForm.getUnitUserName());
		info.setUnitUserSex(uForm.getUnitUserSex());
		info.setUnitUserDepart(uForm.getUnitUserDepart());
		info.setUnitUserDuty(uForm.getUnitUserDuty());
		dao.merge(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "UnitUser.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getUnitUserName()));
		logDao.save(s);
		return new ActionForward("/UnitUser.do?method=list&pages="+request.getParameter("pages"), true);
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UnitUserForm uForm = (UnitUserForm)form;
		UnitUser info = new UnitUser();
		EmployerInfoDAO eDao = new EmployerInfoDAO(request, response);
		UnitUserDAO dao = new UnitUserDAO(request, response);
		EmployerInfo eInfo = eDao.findById(uForm.getUnitUserUnitId());
		/*List<EmployerInfo> eList = dao.findAllfromEmployerInfoId(eInfo.getEmpInfoId());
		 if(eList != null && eList.size() > 0){
			 request.setAttribute("error","此单位下已存在用户,不能添加多个!" );
			return mapping.findForward("error");
		 }*/
		//查找已经注册过的登录名
		UnitUserRegisterDAO registerDAO = new UnitUserRegisterDAO(request, response);
		List<UnitUser> registerList = registerDAO.findAllfromLoginName(uForm.getUnitUserLoginName());
		 if(registerList != null && registerList.size() > 0){
			 request.setAttribute("error","登陆名名已存在！请重新输入其它登陆名！" );
			return mapping.findForward("error");
		 }
		info.setUnitUser(eInfo);
		info.setUnitUserLoginName(uForm.getUnitUserLoginName());
		info.setUnitUserPassword(uForm.getUnitUserPassword());
		info.setUnitUserName(uForm.getUnitUserName());
		info.setUnitUserSex(uForm.getUnitUserSex());
		info.setUnitUserDepart(uForm.getUnitUserDepart());
		info.setUnitUserDuty(uForm.getUnitUserDuty());
		dao.save(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("0");
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "UnitUser.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getUnitUserName()));
		logDao.save(s);
		return mapping.findForward("error");
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UnitUserDAO dao = new UnitUserDAO(request, response);
		UnitUser info = dao.findId(request.getParameter("uid"));
		dao.delete(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "UnitUser.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getUnitUserName()));
		logDao.save(s);
		return new ActionForward("/UnitUser.do?method=list&pages="+request.getParameter("page"), true);
	}
	
	
}